//package com.cus.shd.sharingjdbc.customShardingAlgorithm;
//
//import com.google.common.base.Preconditions;
//import groovy.lang.Closure;
//import groovy.util.Expando;
//import lombok.Getter;
//import lombok.Setter;
//import org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineExpressionParser;
//import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
//import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
//import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
//
//import java.util.Collection;
//import java.util.Properties;
//
//public class MyInlineShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
//
//    private static final String ALGORITHM_EXPRESSION_KEY = "algorithm-expression";
//
//    private static final String ALLOW_RANGE_QUERY_KEY = "allow-range-query-with-inline-sharding";
//
//    private boolean allowRangeQuery;
//
//    @Getter
//    @Setter
//    private Properties props = new Properties();
//
//    @Override
//    public void init() {
//        allowRangeQuery = isAllowRangeQuery();
//    }
//
//    private Closure<?> createClosure() {
//        String expression = props.getProperty(ALGORITHM_EXPRESSION_KEY);
//        Preconditions.checkNotNull(expression, "Inline sharding algorithm expression cannot be null.");
//        String algorithmExpression = InlineExpressionParser.handlePlaceHolder(expression.trim());
//        Closure<?> result = new InlineExpressionParser(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
//        result.setResolveStrategy(Closure.DELEGATE_ONLY);
//        return result;
//    }
//
//    private boolean isAllowRangeQuery() {
//        return Boolean.parseBoolean(props.getOrDefault(ALLOW_RANGE_QUERY_KEY, Boolean.FALSE.toString()).toString());
//    }
//
//    @Override
//    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
//        Closure<?> closure = createClosure();
//        closure.setProperty(shardingValue.getColumnName(), shardingValue.getValue());
//        return closure.call().toString();
//    }
//
//    @Override
//    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
//        if (allowRangeQuery) {
//            return availableTargetNames;
//        }
//        throw new UnsupportedOperationException("Since the property of `" + ALLOW_RANGE_QUERY_KEY + "` is false, inline sharding algorithm can not tackle with range query.");
//    }
//
//    @Override
//    public String getType() {
//        return "MYINLINE";
//    }
//}
